Imports System.Data.SqlClient


Partial Class productslist
    Inherits System.Web.UI.Page
    Dim objrd As SqlDataReader = Nothing
    Dim objrdCom As SqlDataReader = Nothing
    Private i As Integer
    Private pages As Integer

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim sId As String
        Dim cId As String
        cId = Request.QueryString("cId")
        Dim sName As String
        Dim cName As String
        Dim comName, country, countryCode, strErr As String
        comName = ""
        strErr = ""
        Dim comId As String
        sId = Request.QueryString("sid")
        Dim strProduct As String
        strProduct = ""
        Dim strSubCategory As String
        strSubCategory = ""
        Dim objdt As New GetData
        Dim objdtCom As New GetData
        Dim ps As New PreparedStatement("")
        Dim psc As New PreparedStatement("")

        Dim photo_path As String = ""

        Dim sum, sumdesc As Integer
        i = Request("page")
        If i = Nothing Or i = 0 Then
            i = 1
        End If

        If sId <> Nothing And sId <> "" Then

            Dim conn As SqlConnection = objdt.getConn()
            sidNum.Value = sId.ToString
            cidNum.Value = ""
            ps.strSql = "select * from sub_category where sub_category_id = @sId"
            ps.addParameter("sId", sId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            If objrd.HasRows Then
                objrd.Read()
                sName = objrd.Item("sub_category_name").ToString
                cId = objrd.Item("category_id").ToString
                ps.strSql = "select * from category where category_id = @cid"
                ps.addParameter("cid", objrd.Item("category_id"))
                objrd = objdt.dr(ps, strErr, conn, objrd)
                cName = ""
                If objrd.HasRows Then
                    objrd.Read()
                    cName = objrd.Item("category_name").ToString
                End If
                levelPathBar.InnerHtml &= "<a href=""productslist.aspx?cId=" & cId & """ >" & cName & "</a>&nbsp;&gt; " & _
                        "<b>" & sName & "</b>"
            End If
            ps.strSql = "select count(*) from product_info where sub_category_id = @sId and lang_flag='en'"
            ps.parameters.Clear()
            ps.addParameter("sId", sId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            objrd.Read()
            sum = objrd.Item(0)
            pages = (sum \ 10) + 1
            If i >= pages Then
                i = pages
            ElseIf i <= 0 Then
                i = 1
            End If
            If (i = pages) Then
                sumdesc = sum
            Else
                sumdesc = i * 10
            End If
            totalPage.InnerText = i.ToString & "/" & pages.ToString()
            totalPage1.InnerText = i.ToString & "/" & pages.ToString()
            resultNum.InnerText = sum.ToString & "Results"
            ps.strSql = "select top(" & i & "*10) * from product_info where sub_category_id = @sId and lang_flag='en' " & _
                    "and product_id not in (" & _
                    "select top (" & i - 1 & "*10) product_id from product_info where sub_category_id = @sId and lang_flag='en' " & _
                    "order by product_name" & _
                    ") and product_id not in (select top(" & sum - sumdesc & ") product_id from product_info where sub_category_id = @sId and lang_flag='en' " & _
                    "order by product_name desc) order by product_name"
            'ps.strSql = "select * from product_info where sub_category_id = @sId order by Product_name"
            ps.addParameter("sId", sId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            If objrd.HasRows Then
                Dim con As SqlConnection = objdtCom.getConn()

                While (objrd.Read())
                    comId = objrd.Item("company_id")
                    psc.strSql = "select * from merchant_info where company_id = @comId order by company_name"
                    psc.addParameter("comId", comId)
                    objrdCom = objdtCom.dr(psc, strErr, con, objrdCom)
                    If objrdCom.HasRows Then
                        objrdCom.Read()
                        comName = objrdCom.Item("company_name").ToString
                        country = objrdCom.Item("country").ToString
                        If (country.ToLower.Equals("china")) Then
                            countryCode = "../assets/cn.gif"
                        ElseIf (country.ToLower.Equals("usa")) Then
                            countryCode = "../assets/us.gif"
                        End If
                    End If
                    If (objrd.Item("photo_path").ToString = Nothing) Then
                        photo_path = "/pic/nopic180.gif"
                    Else
                        photo_path = objrd.Item("photo_path").ToString
                    End If
                    strProduct &= "<div class=""itemBox"">" & _
                        "<div class=""box2"">" & _
                            "<div class=""images"">" & _
                                "<a target=""_blank"" dot=""area=ml2&type;=pimg&value;="" value=""#"">" & _
                                    "<img  border=""0"" isoem=""true"" alt=""" & objrd.Item("Product_name") & """ src=""" & photo_path & """ width=""100px"" height=""75px""/>" & _
                                "</a>" & _
                            "</div>" & _
                        "</div>" & _
                        "<div class=""box5 products"">" & _
                            "<div style=""margin-bottom: 5px;"">" & _
                                "Supplier:" & _
                                "<a class=""company"" target=""_blank"" dot=""area=ml2&type;=pttl&value;="" href=""company.aspx?cId=" & comId & """>" & comName & "</a>" & _
                            "</div>" & _
                            "<div class=""location"">" & _
                                "<img style=""border: 0pt none; vertical-align: top;"" alt=""country flag"" src=""" & countryCode & """/>" & _
                                country & _
                            "</div>" & _
                        "</div>" & _
                        "<div class=""box3"">" & _
                            "<div class=""productname"">" & _
                                "<a  target=""_blank"" title="""" dot=""area=ml2&type;=pttl&value;="" href=""productdetail.aspx?productId=" & objrd.Item("product_id") & """>" & _
                                objrd.Item("Product_name") & _
                                "</a>" & _
                            "</div>" & _
                            "<div class=""description"">" & _
                                objrd.Item("description") & _
                                "<br>" & _
                            "</div>" & _
                        "</div>" & _
                    "</div>"
                End While
                objdtCom.closeConn(con)
            End If
            objdt.closeConn(conn)
            prePage.HRef = "productslist.aspx?sid=" & sId & " & page=" & i - 1
            nextPage.HRef = "productslist.aspx?sid=" & sId & " & page=" & i + 1
            productList.InnerHtml = strProduct
            subcategory.InnerHtml = strSubCategory
        ElseIf cId <> Nothing And cId <> "" Then
            Dim conn As SqlConnection = objdt.getConn()
            cidNum.Value = cId.ToString
            sidNum.Value = ""
            ps.strSql = "select * from category where category_id =@cId"
            ps.addParameter("cId", cId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            If objrd.HasRows Then
                objrd.Read()
                cName = objrd.Item("category_name").ToString
                levelPathBar.InnerHtml &= "<b>" & cName & "</b>"
            End If
            ps.strSql = "select * from sub_category where category_id = @cId order by sub_category_name"
            ps.addParameter("cId", cId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            If objrd.HasRows Then
                While (objrd.Read())
                    strSubCategory &= "<li>" & _
                                        "<a href=""productslist.aspx?sId=" & objrd.Item("sub_category_id") & """>" & objrd.Item("sub_category_name") & "</a>" & _
                                    "</li>"

                End While
                strSubCategory = "<div  class=""subcategory"" >	" & _
                                "<ul>" & _
                                    strSubCategory & _
                                "</ul>" & _
                            "</div>"
            End If

            ps.strSql = "select count(*) from product_info where category_id = @cId and lang_flag='en'"
            ps.parameters.Clear()
            ps.addParameter("cId", cId)
            objrd = objdt.dr(ps, strErr, conn, objrd)
            objrd.Read()
            sum = objrd.Item(0)
            pages = (sum \ 10) + 1
            If i >= pages Then
                i = pages
            ElseIf i <= 0 Then
                i = 1
            End If
            If (i = pages) Then
                sumdesc = sum
            Else
                sumdesc = i * 10
            End If
            totalPage.InnerText = i.ToString & "/" & pages.ToString()
            totalPage1.InnerText = i.ToString & "/" & pages.ToString()
            resultNum.InnerText = sum.ToString & "Results"
            ps.strSql = "select top(" & i & "*10) * from product_info where category_id = @cId and lang_flag='en' " & _
                    "and product_id not in (" & _
                    "select top (" & i - 1 & "*10) product_id from product_info where category_id = @cId and lang_flag='en' " & _
                    "order by product_name" & _
                    ") and product_id not in (select top(" & sum - sumdesc & ") product_id from product_info where category_id = @cId and lang_flag='en' " & _
                    "order by product_name desc) order by product_name"

            'ps.strSql = "select * from product_info where category_id = @cId order by Product_name"
            ps.addParameter("cId", cId)
            objrd = objdt.dr(ps, strErr, conn, objrd)

            If objrd.HasRows Then
                Dim con As SqlConnection = objdtCom.getConn()

                While (objrd.Read())
                    comId = objrd.Item("company_id")
                    psc.strSql = "select * from merchant_info where company_id = @comId order by company_name"
                    psc.addParameter("comId", comId)
                    objrdCom = objdtCom.dr(psc, strErr, con, objrdCom)
                    If objrdCom.HasRows Then
                        objrdCom.Read()
                        comName = objrdCom.Item("company_name").ToString
                        country = objrdCom.Item("country").ToString
                        If (country.ToLower.Equals("china")) Then
                            countryCode = "../assets/cn.gif"
                            country = "China"
                        ElseIf (country.ToLower.Equals("usa")) Then
                            countryCode = "../assets/us.gif"
                            country = "USA"
                        End If
                    End If

                    If (objrd.Item("photo_path").ToString = Nothing) Then
                        photo_path = "/pic/nopic180.gif"
                    Else
                        photo_path = objrd.Item("photo_path").ToString
                    End If
                    strProduct &= "<div class=""itemBox"">" & _
                                "<div class=""box2"">" & _
                                    "<div class=""images"">" & _
                                        "<a target=""_blank"" dot=""area=ml2&type;=pimg&value;="" value=""#"">" & _
                                            "<img  border=""0"" isoem=""true"" alt=""" & objrd.Item("Product_name") & """ src=""" & photo_path & """ width=""100px"" height=""75px""/>" & _
                                        "</a>" & _
                                    "</div>" & _
                                "</div>" & _
                                "<div class=""box5 products"">" & _
                                    "<div style=""margin-bottom: 5px;"">" & _
                                        "Supplier:" & _
                                        "<a class=""company"" target=""_blank"" dot=""area=ml2&type;=pttl&value;="" href=""company.aspx?cId=" & comId & """>" & comName & "</a>" & _
                                    "</div>" & _
                                    "<div class=""location"">" & _
                                        "<img style=""border: 0pt none; vertical-align: top;"" alt=""country flag"" src=""" & countryCode & """/>" & _
                                        country & _
                                    "</div>" & _
                                "</div>" & _
                                "<div class=""box3"">" & _
                                    "<div class=""productname"">" & _
                                        "<a  target=""_blank"" title="""" dot=""area=ml2&type;=pttl&value;="" href=""productdetail.aspx?productId=" & objrd.Item("product_id") & """>" & _
                                        objrd.Item("Product_name") & _
                                        "</a>" & _
                                    "</div>" & _
                                    "<div class=""description"">" & _
                                        objrd.Item("description") & _
                                        "<br>" & _
                                    "</div>" & _
                                "</div>" & _
                            "</div>"
                End While
                objdtCom.closeConn(con)
            End If
            objdt.closeConn(conn)
            prePage.HRef = "productslist.aspx?cId=" & cId & "&page=" & i - 1
            nextPage.HRef = "productslist.aspx?cId=" & cId & "&page=" & i + 1
            productList.InnerHtml = strProduct
            subcategory.InnerHtml = strSubCategory
        Else
            Response.Redirect("category_default.aspx")
        End If


    End Sub

    Protected Sub go_Click(ByVal sender As Object, ByVal e As EventArgs) Handles go.Click
        If (sidNum.Value <> Nothing And sidNum.Value <> "") Then
            Response.Redirect("productslist.aspx?sId=" & sidNum.Value & "&page=" & pageNum.Text)
        ElseIf (cidNum.Value <> Nothing And cidNum.Value <> "") Then
            Response.Redirect("productslist.aspx?cId=" & cidNum.Value & "&page=" & pageNum.Text)
        End If

    End Sub
End Class
